﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rankep.FilterableDataGridView;

namespace DiveManager
{
    public partial class FindGuideForm : Form
    {
        protected DiveDatabaseEntities diveDatabase;

        // Item Filter
        private FilterItem textboxFilter;

        // Properties
        public Guid SelectedGuide { get; set; }

        public FindGuideForm()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();
        }

        private void FindGuide_Load(object sender, EventArgs e)
        { 
            // Load list
            LoadData();

            // Add Filtering
            textboxFilter = new FilterItem("", "FirstName|LastName");
        }

        protected void LoadData()
        {
            IQueryable<DiveGuides> dataQuery = (from a in diveDatabase.DiveGuides
                                                select a);

            // Bind BindingSource to List
            GuidesList.DataSource = dataQuery;

            // Hide ID-Columns
            GuidesList.Columns["ID"].Visible = false;
            GuidesList.Columns["RelationGuidesActivities"].Visible = false;

            // Set Column Headers
            GuidesList.Columns["FirstName"].HeaderText = "Vorname";
            GuidesList.Columns["LastName"].HeaderText = "Nachname";
        }

        private void txGuideFilter_TextChanged(object sender, EventArgs e)
        {
            //Change the filter Text
            textboxFilter.Filter = txGuideFilter.Text;

            GuidesList.BeginFilterUpdate();
            try
            {
                //Add the filter
                GuidesList.Filters.Add(textboxFilter);
            }
            finally
            {
                GuidesList.EndFilterUpdate();
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            // Nothing selected
            SelectedGuide = Guid.Empty;

            Close();
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void GuidesList_SelectionChanged(object sender, EventArgs e)
        {
            // Get Current Selection
            DataGridViewSelectedRowCollection selectedRows = GuidesList.SelectedRows;

            if (selectedRows.Count == 1)
            {
               DiveGuides selGuide = (DiveGuides)selectedRows[0].DataBoundItem;
               SelectedGuide = selGuide.ID;
            }
        }

        private void GuidesList_DoubleClick(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;

            Close();
        }

        private void FindGuide_Shown(object sender, EventArgs e)
        {
            txGuideFilter.Focus();
        }

        
    }
}
